<?php

if (!defined('IN_IA')) {
	exit('Access Denied');
}

class Order_EweiShopV2Page extends PluginWebPage
{
	public function main() {
		global $_W;
		global $_GPC;
		$pindex = max(1, intval($_GPC['page']));
		$psize = 20;
		$params = array(':uniacid' => $_W['uniacid']);
		$condition = '';
		$keyword = trim($_GPC['keyword']);

		// 身份等级
		$identity = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));

		if (!empty($keyword)) {
			$condition .= ' and ( io.name like :keyword or io.shop_user_grade like :keyword)';
			$params[':keyword'] = '%' . $keyword . '%';
		}

		$status = empty($_GPC['status']) ? '' : intval($_GPC['status']);
		switch ($status) {
			case '1'://已审核，使用中
				$condition .= ' and io.status = 1 ';
				break;
			case '2'://待审核
				$condition .= ' and io.status = 0 ';
				break;
			case '3'://退单申请
				$condition .= ' and io.status = 2 ';
				break;
			case '4'://已退单
				$condition .= ' and io.status = 3 ';
				break;
			default:
				$condition .= '';
				break;
		}

		if(!empty($_GPC['identity_id'])){
			$condition .= ' AND io.identity_id = :identity_id';
			$params[':identity_id'] = $_GPC['identity_id']; 
		}

		$sql = 'SELECT io.*,o.status as ostatus FROM ' . tablename('ewei_shop_identity_order') . ' io LEFT JOIN '.tablename('ewei_shop_order').' o ON o.ordersn = io.orderssn WHERE io.uniacid = :uniacid ' . $condition . ' ORDER BY io.buy_time DESC';

		if (empty($_GPC['export'])) {
			$sql .= ' limit ' . (($pindex - 1) * $psize) . ',' . $psize;
		}
		$list = pdo_fetchall($sql, $params);
		$total = pdo_fetchcolumn('select count(*) from' . tablename('ewei_shop_identity_order') . ' io LEFT JOIN '.tablename('ewei_shop_order').' o ON o.ordersn = io.orderssn where io.uniacid = :uniacid ' . $condition, $params);

		$ostatus = array('0' => '待付款', '1' => '已付款', '2' => '已发货', '3' => '已完成', '-1' => '取消订单');
		$status = array('0' => '待确认', '1' => '已确认');
		foreach ($list as $key => $value) {
			$list[$key]['buy_time'] = date('Y-m-d', $value['buy_time']);
			$list[$key]['ostatus']  = $ostatus[$value['ostatus']];
			$list[$key]['status']	= $status[$value['status']];
			
			if(empty($list[$key]['ostatus'])){
				$list[$key]['ostatus'] = '无效订单';
				$list[$key]['status'] = '无效订单';
			}
		}



		if ($_GPC['export'] == '1') {
			ca('identity.user.export');
			plog('identity.user.export', '导出订单数据');

			foreach ($variable as $key => $value) {
				# code...
			}

			unset($row);
			m('excel')->export($list, array(
				'title'   => '身份升级订单数据-' . date('Y-m-d-H-i', time()),
				'columns' => array(
					array('title' => 'ID', 'field' => 'id', 'width' => 12),
					array('title' => '会员昵称', 'field' => 'nickname', 'width' => 24),
					array('title' => '升级身份', 'field' => 'name', 'width' => 12),
					array('title' => '升级金额', 'field' => 'goods_price', 'width' => 12),
					array('title' => '升级订单号', 'field' => 'orderssn', 'width' => 12),
					array('title' => '会员身份', 'field' => 'shop_user_grade', 'width' => 12),
					array('title' => '分销身份', 'field' => 'distribution_grade', 'width' => 12),
					array('title' => '起始日期', 'field' => 'start_time', 'width' => 12),
					array('title' => '到期日期', 'field' => 'end_time', 'width' => 12),
					array('title' => '门店功能', 'field' => 'mendian', 'width' => 12),
					array('title' => '状态', 'field' => 'status', 'width' => 12)
					)
				));
		}

		$pager = pagination2($total, $pindex, $psize);
		load()->func('tpl');
		include $this->template();
	}

	public function status()
	{
		global $_W;
		global $_GPC;
		$id = intval($_GPC['id']);
		$status = intval($_GPC['status']);

		if (empty($id)) {
			$id = (is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0);
		}

		$items = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_identity_order') . ' WHERE id in( ' . $id . ' ) AND uniacid=' . $_W['uniacid']);

		
		// 会员等级
		$member_level = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_member_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($member_level as $key => $value) {
			$user[$value['id']] = $value;
		}

		// 分销等级
		$commission = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_commission_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($commission as $key => $value) {
			$distribution[$value['id']] = $value;
		}

		foreach ($items as $item) {
			$order = pdo_fetch('SELECT status FROM ' . tablename('ewei_shop_order') . ' WHERE uniacid = :uniacid AND ordersn = :ordersn', array(':uniacid' => $_W['uniacid'], ':ordersn' => $item['orderssn']));

			if( $order['status'] == '1' ){
				pdo_update('ewei_shop_identity_order', array('status' => intval($_GPC['status'])), array('id' => $item['id']));

				$identity = pdo_fetch('select * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $item['identity_id']));
				$identity_member = pdo_fetch('SELECT * FROM '.tablename('ewei_shop_identity_member').' WHERE uniacid = :uniacid AND shop_user_id = :shop_user_id', array(':uniacid' => $_W['uniacid'], ':shop_user_id' => $item['uid']));
				if($item['status'] == '0' && empty($identity_member['start_time'])){
					$item['start_time'] = time();
				}else{
					$item['start_time'] = $identity_member['start_time'];
				}
				if($item['status'] == '0'){
					if( $identity_member['end_time'] > time()){
						$item['end_time'] = $identity_member['end_time'] + ($identity['useful_time'] * 86400 * $item['goods_total']);
					}else{
						$item['end_time'] = time() + ($identity['useful_time'] * 86400 * $item['goods_total']);
					}
				} else {
					$item['end_time'] = $identity_member['end_time'];
				}
				
				if($status == '1'){
					$data['content'] = '用户' . $item['user_name'] . '的身份'.$item['identity'] . '启用';
					$data['status'] = '1';
				} else {
					$data['content'] = '用户' . $item['user_name'] . '的身份'.$item['identity'] . '停用';
					$data['status'] = '2';
				}
				$shop_user_grade_id 	   = intval($identity['shop_user_grade_id']);
				$shop_user_grade 		   = $user[$shop_user_grade_id]['levelname'];//会员等级
				$end_shop_user_grade_id    = intval($identity['end_shop_user_grade_id']);
				$end_shop_user_grade 	   = $user[intval($identity['end_shop_user_grade_id'])]['levelname'];//到期会员等级
				$distribution_grade_id 	   = intval($identity['distribution_grade_id']);
				$distribution_grade 	   = $distribution[$distribution_grade_id]['levelname'];//分销等级
				$end_distribution_grade_id = intval($identity['end_distribution_grade_id']);
				$end_distribution_grade    = $distribution[$end_distribution_grade_id]['levelname'];//到期分销等级

				// 修改会员身份
				$info = array(
					'is_use_member_level'		=> $identity['is_use_member_level'],
					'shop_user_grade_id' 		=> $shop_user_grade_id,
					'shop_user_grade'    		=> $shop_user_grade,
					'end_shop_user_grade_id'	=> $end_shop_user_grade_id,
					'end_shop_user_grade'   	=> $end_shop_user_grade,
					'is_use_distribution_level' => $identity['is_use_distribution_level'],
					'distribution_grade_id' 	=> $distribution_grade_id,
					'distribution_grade'		=> $distribution_grade,
					'end_distribution_grade_id' => $end_distribution_grade_id,
					'end_distribution_grade'	=> $end_distribution_grade,
					'start_time'				=> $item['start_time'],
					'end_time'					=> $item['end_time'],
					'identity'					=> $item['identity'],
					'identity_id'				=> $item['identity_id'],
					'buy_goods_id'				=> $item['goods_id'],
					'buy_goods_title'			=> $item['goods_name'],
					'buy_goods_price'			=> $item['goods_price'],
					'is_use_merch'				=> $identity['is_use_merch'],
					'merch_id'					=> $identity['merch_id'],
					'end_merch_id'				=> $identity['end_merch_id'],
					'is_use_abonus_agent'		=> $identity['is_use_abonus_agent'],
					'abonus_agent_id'			=> $identity['abonus_agent_id'],
					'end_abonus_agent_id'		=> $identity['end_abonus_agent_id'],
					'is_use_globonus_partner'	=> $identity['is_use_globonus_partner'],
					'globonus_partner_id'		=> $identity['globonus_partner_id'],
					'end_globonus_partner_id'	=> $identity['end_globonus_partner_id'],
					'is_use_mendian'			=> $identity['is_use_mendian'],
					'mendian_level_id'			=> $identity['mendian_level_id'],
					'end_mendian_level_id'		=> $identity['end_mendian_level_id'],
				);
				pdo_update('ewei_shop_identity_member', $info, array('shop_user_id' => $item['uid']));

				$data['uniacid'] 		= $_W['uniacid'];
				$data['uid'] 			= $item['uid'];
				$data['add_time'] 		= time();
				$data['end_time'] 		= $item['end_time'];
				$data['old_identity_id']= $identity_member['identity_id'];
				$data['old_identity'] 	= $identity_member['identity'];
				$data['identity_id'] 	= $identity_member['id'];//会员身份id
				$data['identity_level'] = $item['identity_id'];//会员身份等级id
				pdo_insert('ewei_shop_identity_plog', $data);

				plog('identity.order.edit', ('修改订单状态<br/>ID: ' . $item['id'] . '<br/>用户名称: ' . $item['user_name'] . '<br/>状态: ' . $_GPC['status']) == 1 ? '确认' : '待确认');

				// 用户表修改 ewei_shop_member
				$mdata = array(
					'level' 		=> $shop_user_grade_id, 
					'isagent' 		=> $identity['is_use_distribution_level'], //是否分销商
					'agentlevel' 	=> $distribution_grade_id, 
					'status'		=> 0,
					'ispartner' 	=> $identity['is_use_globonus_partner'], //是否股东
					'partnerlevel' 	=> $identity['globonus_partner_id'], 
					'partnerstatus' => 0,
					'isaagent' 		=> $identity['is_use_abonus_agent'], //是否区域代理
					'aagentlevel' 	=> $identity['abonus_agent_id'],
					'aagentstatus'	=> 0,
				);
				pdo_update('ewei_shop_member', $mdata, array('uniacid' => $_W['uniacid'], 'uid' => $item['uid']));
				// 多商户用户表 ewei_shop_merch_user
				$mudata = array(
					'uniacid'=> $_W['uniacid'],
					'openid' => $item['openid'],
					'groupid' => $identity['merch_id'],
					'status' => 0,
				);
				$merchuser = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_merch_user'). ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], 'openid' => $item['openid']));
				if(empty($merchuser)){
					$mudata['jointime'] = time();
					pdo_insert('ewei_shop_merch_user', $mudata);
				}else{
					pdo_update('ewei_shop_merch_user', $mudata, array('uniacid' => $_W['uniacid'], 'openid' => $item['openid']));
				}
				// 门店用户表 ewei_shop_mendian_info
				$mddata = array(
					'uniacid'=> $_W['uniacid'],
					'uid' => $item['uid'],
					'mendian_level' => $identity['mendian_level_id'],
					'start_time' => $info['start_time'],
					'end_time' => $end_time,
					'status' => 0,
				);
				$mendian = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_info'). ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $item['uid']));
				$set = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_level') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $identity['mendian_level_id']));
				$mdset = array(
					'uniacid'			=> $_W['uniacid'],
					'is_show_diy_menu'  => $set['is_show_diy_menu'],
					'is_show_mobile'	=> $set['is_show_mobile'],
					'is_show_message'	=> $set['is_show_message'],
					'is_show_wechat'	=> $set['is_show_wechat'],
					'is_show_remarks'	=> $set['is_show_remarks'],
					'is_show_address'	=> $set['is_show_address'],
					'remarks'			=> $set['remarks'],
					'status'			=> 0,
					'is_show_type'		=> $set['is_show_type'],
				);
				if(empty($mendian)){
					pdo_insert('ewei_shop_mendian_info', $mddata);
					$mendian_id = pdo_insertid();
					$mdset['mendian_id'] = $mendian_id;
					pdo_insert('ewei_shop_mendian_setting', $mdset);
				}else{
					pdo_update('ewei_shop_mendian_info', $mddata, array('uniacid' => $_W['uniacid'], 'uid' => $item['uid']));
					$setting = pdo_fetch('SELECT * FROM '.tablename('ewei_shop_mendian_setting').' WHERE uniacid = :uniacid AND mendian_id = :mendian_id', array(':uniacid' => $_W['uniacid'], ':mendian_id' => $mendian['id']));
					if(empty($setting))
					{
						$mdset['mendian_id'] = $mendian['id'];
						pdo_insert('ewei_shop_mendian_setting', $mdset);
					} else {
						pdo_update('ewei_shop_mendian_setting', $mdset, array('uniacid' => $_W['uniacid'], 'mendian_id' => $mendian['id']));
					}
				}
			}
		}

		show_json(1, array('url' => referer()));
	}

	public function editidentity() {
		global $_W;
		global $_GPC;
		$id = intval($_GPC['id']);

		if (empty($id)) {
			$id = (is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0);
		}

		$items = pdo_fetchall('SELECT id,user_name,identity FROM ' . tablename('ewei_shop_identity_order') . ' WHERE id in( ' . $id . ' ) AND uniacid=' . $_W['uniacid'] . ' AND status = 1');

		$identity = pdo_fetch('select id,name FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND id = :id',array(':uniacid' => $_W['uniacid'], ':id' => $_GPC['identity_id']));

		foreach ($items as $item) {
			pdo_update('ewei_shop_identity_order', array('identity_id' => intval($_GPC['identity_id']), 'identity' => $identity['name']), array('id' => $item['id']));
			plog('merch.category.edit', ('修改订单身份等级<br/>ID: ' . $item['id'] . '<br/>用户名称: ' . $item['user_name'] . '<br/>身份等级: ' . $_GPC['identity']) . ' -> ' . $identity['name']);
		}

		show_json(1, array('url' => referer()));
	}

	public function edit()
	{
		$this->post();
	}

	protected function post()
	{
		global $_W;
		global $_GPC;
		$id = intval($_GPC['id']);
		$area_set = m('util')->get_area_config_set();
		$new_area = intval($area_set['new_area']);

		$item = pdo_fetch('select * from ' . tablename('ewei_shop_identity_order') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $id, ':uniacid' => $_W['uniacid']));
		$order = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_order') . ' WHERE uniacid = :uniacid AND ordersn = :ordersn', array(':ordersn' => $item['orderssn'], ':uniacid' => $_W['uniacid']));

		$identity = pdo_fetch('select * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $item['identity_id']));
		$identity_member = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity_member') . ' WHERE uniacid = :uniacid AND shop_user_id = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $item['uid']));

		// if($item['status'] == '0'){
		// 	if($identity_member['end_time'] < time()){
		// 		$item['start_time'] = time();
		// 	} else{
		// 		$item['start_time'] = $identity_member['end_time'];
		// 	}
		// }else{
		// 	$item['start_time'] = $item['start_time'];
		// }
		if($item['status'] == '0'){
			// 购买的身份与当前身份是否一致
			if($identity_member['identity_id'] == $item['identity_id']){

				// 身份未到期
				if( $identity_member['end_time'] > time()){
					$item['start_time'] = $identity_member['end_time'];
					$item['end_time'] = $identity_member['end_time'] + ($identity['useful_time'] * 86400 * $item['goods_total']);
				}else{
					$item['start_time'] = time();
					$item['end_time'] = time() + ($identity['useful_time'] * 86400 * $item['goods_total']);
				}
			} else {
				$item['start_time'] = time();
				$item['end_time'] = time() + ($identity['useful_time'] * 86400 * $item['goods_total']);
			}
		} else {
			$item['start_time'] = $identity_member['start_time'];
			$item['end_time'] = $identity_member['end_time'];
		}
		

		// 会员等级
		$member_level = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_member_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($member_level as $key => $value) {
			$user[$value['id']] = $value;
		}

		// 分销等级
		$commission = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_commission_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($commission as $key => $value) {
			$distribution[$value['id']] = $value;
		}
		
		// 门店等级
		$mendian = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_mendian_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($mendian as $key => $value) {
			$mendian_level[$value['id']] = $value;
		}

		// 多商户分组
		$merch = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_merch_group') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($merch as $key => $value) {
			$merch_level[$value['id']] = $value;
		}

		// 全民股东等级
		$globonus = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_globonus_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($globonus as $key => $value) {
			$globonus_level[$value['id']] = $value;
		}

		// 区域代理等级
		$abonus = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_abonus_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($abonus as $key => $value) {
			$abonus_level[$value['id']] = $value;
		}
		
		
		if ($_W['ispost']) {

			$id = $_GPC['id'];
			$shop_user_grade_id 	   = intval($_GPC['shop_user_grade_id']);
			$shop_user_grade 		   = $user[$shop_user_grade_id]['levelname'];//会员等级
			$end_shop_user_grade_id    = intval($_GPC['end_shop_user_grade_id']);
			$end_shop_user_grade 	   = $user[intval($_GPC['end_shop_user_grade_id'])]['levelname'];//到期会员等级
			$distribution_grade_id 	   = intval($_GPC['distribution_grade_id']);
			$distribution_grade 	   = $distribution[$distribution_grade_id]['levelname'];//分销等级
			$end_distribution_grade_id = intval($_GPC['end_distribution_grade_id']);
			$end_distribution_grade    = $distribution[$end_distribution_grade_id]['levelname'];//到期分销等级
			$start_time                = strtotime($_GPC['start_time']);
			$end_time				   = strtotime($_GPC['end_time']);

			if($identity_member['end_time'] < time()){

			}

			// 修改会员身份
			$info = array(
				'is_use_member_level'		=> $identity['is_use_member_level'],
				'shop_user_grade_id' 		=> $shop_user_grade_id,
				'shop_user_grade'    		=> $shop_user_grade,
				'end_shop_user_grade_id'	=> $end_shop_user_grade_id,
				'end_shop_user_grade'   	=> $end_shop_user_grade,
				'is_use_distribution_level' => $identity['is_use_distribution_level'],
				'distribution_grade_id' 	=> $distribution_grade_id,
				'distribution_grade'		=> $distribution_grade,
				'end_distribution_grade_id' => $end_distribution_grade_id,
				'end_distribution_grade'	=> $end_distribution_grade,
				'start_time'				=> $item['start_time'],
				'end_time'					=> $end_time,
				'identity'					=> $item['identity'],
				'identity_id'				=> $item['identity_id'],
				'buy_goods_id'				=> $item['goods_id'],
				'buy_goods_title'			=> $item['goods_name'],
				'buy_goods_price'			=> $item['goods_price'],
				'is_use_merch'				=> $identity['is_use_merch'],
				'merch_id'					=> $identity['merch_id'],
				'end_merch_id'				=> $identity['end_merch_id'],
				'is_use_abonus_agent'		=> $identity['is_use_abonus_agent'],
				'abonus_agent_id'			=> $identity['abonus_agent_id'],
				'end_abonus_agent_id'		=> $identity['end_abonus_agent_id'],
				'is_use_globonus_partner'	=> $identity['is_use_globonus_partner'],
				'globonus_partner_id'		=> $identity['globonus_partner_id'],
				'end_globonus_partner_id'	=> $identity['end_globonus_partner_id'],
				'is_use_mendian'			=> $identity['is_use_mendian'],
				'mendian_level_id'			=> $identity['mendian_level_id'],
				'end_mendian_level_id'		=> $identity['end_mendian_level_id'],
			);

			if($identity_member['end_time'] > time()){
				$info['start_time'] = $identity_member['start_time'];
			}else{
				$info['start_time'] = $start_time;
			}

			$status = trim($_GPC['status']);
			if($status == '确认订单'){
				$status = '1';
				$info['status'] = '1';
			}elseif ($status == '确认退单') {
				$status = '3';
				$info['status'] = '0';
			}
			$odata = array(
				'status' => $status,	
				'start_time' => $start_time,
				'end_time' => $end_time,		
			);
			

			pdo_update('ewei_shop_identity_order', $odata, array('id' => $id));
			plog('identity.order.edit', ('修改订单状态<br/>ID: ' . $item['id'] . '<br/>用户名称: ' . $item['user_name'] . '<br/>状态: ' . $_GPC['status']) == 1 ? '确认' : '待确认');

			pdo_update('ewei_shop_identity_member', $info, array('shop_user_id' => $item['uid']));
			plog('identity.member.edit', '修改会员身份信息<br/>ID: ' . $item['shop_user_id'] . '<br/>用户名称: ' . $item['user_name']);
			
			if($status == '1'){
				$data['content'] = '用户' . $item['user_name'] . '的身份'.$item['identity'] . '启用';
				$data['status'] = '1';
			} else {
				$data['content'] = '用户' . $item['user_name'] . '的身份'.$item['identity'] . '停用';
				$data['status'] = '2';
			}
			$data['uniacid'] 		= $_W['uniacid'];
			$data['uid'] 			= $item['uid'];
			$data['status']			= $status;
			$data['add_time'] 		= time();
			$data['end_time'] 		= $end_time;
			$data['old_identity_id']= $identity_member['identity_id'];
			$data['old_identity'] 	= $identity_member['identity'];
			$data['identity_id'] 	= $identity_member['id'];//会员身份id
			$data['identity_level'] = $item['identity_id'];//会员身份等级id
			pdo_insert('ewei_shop_identity_plog', $data);


			// 用户表修改 ewei_shop_member
			$mdata = array(
				'level' 		=> $shop_user_grade_id, 
				'isagent' 		=> $identity['is_use_distribution_level'], //是否分销商
				'agentlevel' 	=> $distribution_grade_id, 
				'status'		=> 0,
				'ispartner' 	=> $identity['is_use_globonus_partner'], //是否股东
				'partnerlevel' 	=> $identity['globonus_partner_id'], 
				'partnerstatus' => 0,
				'isaagent' 		=> $identity['is_use_abonus_agent'], //是否区域代理
				'aagentlevel' 	=> $identity['abonus_agent_id'],
				'aagentstatus'	=> 0,
			);
			pdo_update('ewei_shop_member', $mdata, array('uniacid' => $_W['uniacid'], 'uid' => $item['uid']));
			// 多商户用户表 ewei_shop_merch_user
			$mudata = array(
				'uniacid'=> $_W['uniacid'],
				'openid' => $item['openid'],
				'groupid' => $identity['merch_id'],
				'status' => 0,
			);
			$merchuser = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_merch_user'). ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], 'openid' => $item['openid']));
			if(empty($merchuser)){
				$mudata['jointime'] = time();
				pdo_insert('ewei_shop_merch_user', $mudata);
			}else{
				pdo_update('ewei_shop_merch_user', $mudata, array('uniacid' => $_W['uniacid'], 'openid' => $item['openid']));
			}
			// 门店用户表 ewei_shop_mendian_info
			$mddata = array(
				'uniacid'=> $_W['uniacid'],
				'uid' => $item['uid'],
				'mendian_level' => $identity['mendian_level_id'],
				'start_time' => $info['start_time'],
				'end_time' => $end_time,
				'status' => 0,
			);
			$set = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_level') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $identity['mendian_level_id']));
			$mendian = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_info'). ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $item['uid']));
			$mdset = array(
				'uniacid'			=> $_W['uniacid'],
				'is_show_diy_menu'  => $set['is_show_diy_menu'],
				'is_show_mobile'	=> $set['is_show_mobile'],
				'is_show_message'	=> $set['is_show_message'],
				'is_show_wechat'	=> $set['is_show_wechat'],
				'is_show_remarks'	=> $set['is_show_remarks'],
				'is_show_address'	=> $set['is_show_address'],
				'remarks'			=> $set['remarks'],
				'status'			=> 0,
				'is_show_type'		=> $set['is_show_type'],
			);
			if(empty($mendian)){
				pdo_insert('ewei_shop_mendian_info', $mddata);
				$mendian_id = pdo_insertid();
				$mdset['mendian_id'] = $mendian_id;
				pdo_insert('ewei_shop_mendian_setting', $mdset);
			}else{
				pdo_update('ewei_shop_mendian_info', $mddata, array('uniacid' => $_W['uniacid'], 'uid' => $item['uid']));
				$setting = pdo_fetch('SELECT * FROM '.tablename('ewei_shop_mendian_setting').' WHERE uniacid = :uniacid AND mendian_id = :mendian_id', array(':uniacid' => $_W['uniacid'], ':mendian_id' => $mendian['id']));
				if(empty($setting)) {
					$mdset['mendian_id'] = $mendian['id'];
					pdo_insert('ewei_shop_mendian_setting', $mdset);
				} else {
					pdo_update('ewei_shop_mendian_setting', $mdset, array('uniacid' => $_W['uniacid'], 'mendian_id' => $mendian['id']));
				}
			}
			
			
			show_json(1, array('url' => webUrl('identity/order', array('status' => $data['status']))));
			exit;
		}

		
		
		include $this->template();
	}
}